package org.jash.document.entity

import android.content.Context
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.launcher.ARouter
import kotlinx.coroutines.launch
import org.jash.common.log.logDebug
import org.jash.document.model.DocIntent
import org.jash.document.model.DocViewModel
import org.jash.document.ui.DocActivity
import java.text.SimpleDateFormat
import java.util.Date

private val sdf by lazy { SimpleDateFormat("yyyy-MM-dd HH:mm:ss") }
data class FileEntity(
    val fileId: Int,
    val fileName: String,
    val filePath: String,
    val fileTime: Long,
    val fileUserId: Int,
    val fileVersion: Int
) {
    fun getTimeString():String = sdf.format(Date(fileTime))
    fun delete(context: Context) {
        val docActivity = context as DocActivity
        docActivity.lifecycleScope.launch {
            docActivity.viewModel.intent.send(DocIntent.DelDocuments(fileId))
        }
    }
    fun preview() {
        ARouter.getInstance()
            .build(when(fileName.split(".").last()){
                "pdf" -> "/document/pdf"
                else -> "/document/pdf"
            })
            .withString("url", filePath)
            .withString("fileName", fileName)
            .navigation()
    }
}